package edu.ccut.saturn.studio.wfg.dialog.formdatadialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.dialog.TableViewerContentProvider;
import edu.ccut.saturn.studio.wfg.wfgmodel.ExtendAttributeForFormData;
import edu.ccut.saturn.studio.wfg.wfgmodel.FormDataAttribute;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAttribute;

public class SaturnFormDataDialogForDoubleClick extends TitleAreaDialog {

	private FormDataAttribute formDataAttribute;
	
	private List<SaturnAttribute> attributeList;

	private TableViewer tableViewer;
	
	private Table table;
	
	private Combo typeCombo, accessTypeCombo, requiredCombo;
	
	private Text IDText, valueKeyText, nameText;
	
	private Label IDLabel, nameLabel, valueKeyLabel, typeLabel, accessTypeLabel, requiredLabel;
	
	private Button addButton, modifyButton, deleteButton, upButton, downButton;
	
	public SaturnFormDataDialogForDoubleClick(Shell parentShell, SaturnAttribute attribute) {
		super(parentShell);
		this.formDataAttribute = (FormDataAttribute) attribute;
		this.attributeList = formDataAttribute.getAttributes();
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("EditTableMessage"));   //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		
		Composite area = (Composite) super.createDialogArea(parent);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		final Group group = new Group(area, SWT.NONE);
		group.setLayout(new FormLayout());
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		IDLabel = new Label(group, SWT.NONE);
		final FormData fd_iDLabel = new FormData();
		fd_iDLabel.bottom = new FormAttachment(0, 12);
		fd_iDLabel.top = new FormAttachment(0, 0);
		fd_iDLabel.right = new FormAttachment(0, 70);
		fd_iDLabel.left = new FormAttachment(0, 10);
		IDLabel.setLayoutData(fd_iDLabel);
		IDLabel.setText("ID:");

		IDText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		final FormData fd_iDText = new FormData();
		fd_iDText.top = new FormAttachment(IDLabel, -2, SWT.TOP);
		fd_iDText.left = new FormAttachment(IDLabel, 10, SWT.RIGHT);
		fd_iDText.right = new FormAttachment(100,-10);
		IDText.setLayoutData(fd_iDText);

		valueKeyLabel = new Label(group, SWT.NONE);
		final FormData fd_valueKeyLabel = new FormData();
		fd_valueKeyLabel.top = new FormAttachment(IDLabel, 15, SWT.BOTTOM);
		fd_valueKeyLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_valueKeyLabel.right = new FormAttachment(IDLabel, 0, SWT.RIGHT);
		valueKeyLabel.setLayoutData(fd_valueKeyLabel);
		valueKeyLabel.setText(Messages.getString("Expression"));    //$NON-NLS-1$

		valueKeyText = new Text(group, SWT.BORDER);
		final FormData fd_valueKeyText = new FormData();
		fd_valueKeyText.top = new FormAttachment(valueKeyLabel, -3, SWT.TOP);
		fd_valueKeyText.right = new FormAttachment(IDText, 0, SWT.RIGHT);
		fd_valueKeyText.left = new FormAttachment(IDText, 0, SWT.LEFT);
		valueKeyText.setLayoutData(fd_valueKeyText);

		nameLabel = new Label(group, SWT.NONE);
		final FormData fd_nameLabel = new FormData();
		fd_nameLabel.top = new FormAttachment(valueKeyLabel, 15, SWT.BOTTOM);
		fd_nameLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_nameLabel.right = new FormAttachment(IDLabel, 0, SWT.RIGHT);
		nameLabel.setLayoutData(fd_nameLabel);
		nameLabel.setText(Messages.getString("Name"));    //$NON-NLS-1$

		nameText = new Text(group, SWT.BORDER);
		final FormData fd_nameText = new FormData();
		fd_nameText.top = new FormAttachment(nameLabel, -3, SWT.TOP);
		fd_nameText.left = new FormAttachment(IDText, 0, SWT.LEFT);
		fd_nameText.right = new FormAttachment(IDText, 0, SWT.RIGHT);
		nameText.setLayoutData(fd_nameText);
		
		typeLabel = new Label(group, SWT.NONE);
		final FormData fd_typeLabel = new FormData();
		fd_typeLabel.top = new FormAttachment(nameLabel, 15, SWT.BOTTOM);
		fd_typeLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_typeLabel.right = new FormAttachment(IDLabel, 0, SWT.RIGHT);
		typeLabel.setLayoutData(fd_typeLabel);
		typeLabel.setText(Messages.getString("Type"));    //$NON-NLS-1$

		typeCombo = new Combo(group, SWT.NONE | SWT.READ_ONLY);
		final FormData fd_typeCombo = new FormData();
		fd_typeCombo.top = new FormAttachment(typeLabel, -3, SWT.TOP);
		fd_typeCombo.left = new FormAttachment(IDText, 0, SWT.LEFT);
		fd_typeCombo.right = new FormAttachment(IDText, 0, SWT.RIGHT);
		typeCombo.setLayoutData(fd_typeCombo);
		typeCombo.setItems(SelectionCandidate.formDataTypes);

		accessTypeLabel = new Label(group, SWT.NONE);
		final FormData fd_accessTypeLabel = new FormData();
		fd_accessTypeLabel.top = new FormAttachment(typeLabel, 15, SWT.BOTTOM);
		fd_accessTypeLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_accessTypeLabel.right = new FormAttachment(IDLabel, 0 ,SWT.RIGHT);
		accessTypeLabel.setLayoutData(fd_accessTypeLabel);
		accessTypeLabel.setText(Messages.getString("ReadType"));    //$NON-NLS-1$

		accessTypeCombo = new Combo(group, SWT.NONE | SWT.READ_ONLY);
		final FormData fd_accessTypeCombo = new FormData();
		fd_accessTypeCombo.top = new FormAttachment(accessTypeLabel, -2, SWT.TOP);
		fd_accessTypeCombo.left = new FormAttachment(IDText, 0, SWT.LEFT);
		fd_accessTypeCombo.right = new FormAttachment(IDText, 0 ,SWT.RIGHT);
		accessTypeCombo.setLayoutData(fd_accessTypeCombo);
		accessTypeCombo.setItems(SelectionCandidate.formDataReadWrite);

		requiredLabel = new Label(group, SWT.NONE);
		final FormData fd_requiredLabel = new FormData();
		fd_requiredLabel.top = new FormAttachment(accessTypeLabel, 15, SWT.BOTTOM);
		fd_requiredLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_requiredLabel.right = new FormAttachment(IDLabel, 0 , SWT.RIGHT);
		requiredLabel.setLayoutData(fd_requiredLabel);
		requiredLabel.setText(Messages.getString("IsMust"));    //$NON-NLS-1$

		requiredCombo = new Combo(group, SWT.NONE | SWT.READ_ONLY);
		final FormData fd_requiredCombo = new FormData();
		fd_requiredCombo.top = new FormAttachment(requiredLabel, -1, SWT.TOP);
		fd_requiredCombo.left = new FormAttachment(IDText, 0, SWT.LEFT);
		fd_requiredCombo.right = new FormAttachment(IDText, 0, SWT.RIGHT);
		requiredCombo.setLayoutData(fd_requiredCombo);
		requiredCombo.setItems(SelectionCandidate.formDataRequired);

		final Label extendAttributeLabel = new Label(group, SWT.NONE);
		final FormData fd_extendAttributeLabel = new FormData();
		fd_extendAttributeLabel.top = new FormAttachment(requiredLabel, 40, SWT.BOTTOM);
		fd_extendAttributeLabel.right = new FormAttachment(IDLabel, 0, SWT.RIGHT);
		fd_extendAttributeLabel.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		extendAttributeLabel.setLayoutData(fd_extendAttributeLabel);
		extendAttributeLabel.setText(Messages.getString("ExpandProperty"));    //$NON-NLS-1$

		tableViewer = new TableViewer(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(final DoubleClickEvent arg0) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				ExtendAttributeForFormData attribute = (ExtendAttributeForFormData)selection.getFirstElement();
				openEditOptionDialog(attribute);
				tableViewer.refresh();
			}
		});
		table = tableViewer.getTable();
		final FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(extendAttributeLabel, 5, SWT.BOTTOM);
		fd_table.left = new FormAttachment(IDLabel, 0, SWT.LEFT);
		fd_table.right = new FormAttachment(100,-100);
		fd_table.bottom = new FormAttachment(100, -10);
		table.setLayoutData(fd_table);
		table.setLinesVisible(false);
		table.setHeaderVisible(true);
		
		tableViewer.setContentProvider(new TableViewerContentProvider());
		tableViewer.setLabelProvider(new ExtendAttributeForFormDataLabelProvider());

		final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
		newColumnTableColumn.setWidth(120);
		newColumnTableColumn.setText(Messages.getString("ChooseItem"));    //$NON-NLS-1$

		final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
		newColumnTableColumn_1.setWidth(120);
		newColumnTableColumn_1.setText(Messages.getString("Value"));    //$NON-NLS-1$

		addButton = new Button(group, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				openAddOptionDialog();
			}
		});
		final FormData fd_addButton = new FormData();
		fd_addButton.top = new FormAttachment(table, 5, SWT.TOP);
		fd_addButton.left = new FormAttachment(table, 10, SWT.RIGHT);
		fd_addButton.right = new FormAttachment(100,-10);		
		addButton.setLayoutData(fd_addButton);
		addButton.setText(Messages.getString("Add"));    //$NON-NLS-1$

		modifyButton = new Button(group, SWT.NONE);
		modifyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionevent) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				if (selection.isEmpty()){
					MessageDialog.openInformation(null, SelectionCandidate.HINT, SelectionCandidate.CHOOSE_EDIT_RECORD);  
				} else {
					openEditOptionDialog((ExtendAttributeForFormData)selection.getFirstElement());
					tableViewer.refresh();
				}
			}
		});
		final FormData fd_modifyButton = new FormData();
		fd_modifyButton.top = new FormAttachment(addButton, 5, SWT.BOTTOM);
		fd_modifyButton.left = new FormAttachment(addButton, 0, SWT.LEFT);
		fd_modifyButton.right = new FormAttachment(addButton, 0 , SWT.RIGHT);
		modifyButton.setLayoutData(fd_modifyButton);
		modifyButton.setText(Messages.getString("Alter"));    //$NON-NLS-1$

		deleteButton = new Button(group, SWT.NONE);
		deleteButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionevent) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				if (selection.isEmpty()){
					MessageDialog.openInformation(null, SelectionCandidate.HINT, SelectionCandidate.CHOOSE_DELETE_RECORD);  
				} else {
					ExtendAttributeForFormData extendAttribute = (ExtendAttributeForFormData) selection.getFirstElement();
					attributeList.remove(extendAttribute);
					tableViewer.setInput(attributeList);
				}
			}
		});
		final FormData fd_deleteButton = new FormData();
		fd_deleteButton.top = new FormAttachment(modifyButton, 5, SWT.BOTTOM);
		fd_deleteButton.left = new FormAttachment(modifyButton, 0, SWT.LEFT);
		fd_deleteButton.right = new FormAttachment(modifyButton, 0, SWT.RIGHT);
		deleteButton.setLayoutData(fd_deleteButton);
		deleteButton.setText(SelectionCandidate.DELETE_BUTTON); 

		upButton = new Button(group, SWT.NONE);
		upButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionevent) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				if (selection.isEmpty()){
					MessageDialog.openInformation(null, SelectionCandidate.HINT, SelectionCandidate.CHOOSE_A_RECORD);  
				} else {
					ExtendAttributeForFormData attribute = (ExtendAttributeForFormData)selection.getFirstElement();
					Table table = tableViewer.getTable();
					if (tableViewer.getTable() != null) {
						int i = table.getSelectionIndex();
						if (i > 0) {
							attributeList.remove(attribute);
							attributeList.add(i - 1, attribute);
							tableViewer.setInput(attributeList);
							table.setSelection(i - 1);
						}
					}
				}
			}
		});
		final FormData fd_upButton = new FormData();
		fd_upButton.top = new FormAttachment(deleteButton, 5, SWT.BOTTOM);
		fd_upButton.left = new FormAttachment(deleteButton, 0, SWT.LEFT);
		fd_upButton.right = new FormAttachment(deleteButton, 0, SWT.RIGHT);
		upButton.setLayoutData(fd_upButton);
		upButton.setText(SelectionCandidate.UP_BUTTON); 

		downButton = new Button(group, SWT.NONE);
		downButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionevent) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				if (selection.isEmpty()){
					MessageDialog.openInformation(null, SelectionCandidate.HINT, SelectionCandidate.CHOOSE_A_RECORD);  
				} else {
					ExtendAttributeForFormData attribute = (ExtendAttributeForFormData)selection.getFirstElement();
					Table table = tableViewer.getTable();
					int i = table.getSelectionIndex();
					if (i < tableViewer.getTable().getItemCount() - 1) {
						attributeList.remove(attribute);
						attributeList.add(i + 1, attribute);
						tableViewer.setInput(attributeList);
						table.setSelection(i + 1);
					}
				}
			}
		});
		final FormData fd_downButton = new FormData();
		fd_downButton.top = new FormAttachment(upButton, 5, SWT.BOTTOM);
		fd_downButton.left = new FormAttachment(upButton, 0, SWT.LEFT);
		fd_downButton.right = new FormAttachment(upButton, 0, SWT.RIGHT);
		downButton.setLayoutData(fd_downButton);
		downButton.setText(Messages.getString("DownShift"));    //$NON-NLS-1$
		
		initialize();
		
		return area;
	}

	private void openEditOptionDialog(ExtendAttributeForFormData attr) {
		SaturnFormDataParameterEditDialog editDialog = new SaturnFormDataParameterEditDialog(Display.getDefault().getShells()[0],attr);
		editDialog.open();
	}

	private void openAddOptionDialog() {
		SaturnFormDataParameterAddDialog addDialog = new SaturnFormDataParameterAddDialog(Display.getDefault().getShells()[0],attributeList);
		if (addDialog.open() == IDialogConstants.OK_ID) {
			tableViewer.setInput(attributeList);
		}
	}
	
	private void initialize() {
		IDText.setText(formDataAttribute.getId().trim());
		nameText.setText(formDataAttribute.getName().trim());
		valueKeyText.setText(formDataAttribute.getKey().trim());
		typeCombo.setText(formDataAttribute.getType().trim());
		accessTypeCombo.setText(formDataAttribute.getAccessType().trim());
		requiredCombo.setText(formDataAttribute.getRequired().trim());
		
		if(!"radio".equals(typeCombo.getText())){
			table.setEnabled(false);
			addButton.setEnabled(false);
			modifyButton.setEnabled(false);
			deleteButton.setEnabled(false);
			upButton.setEnabled(false);
			downButton.setEnabled(false);
		}
		tableViewer.setInput(attributeList);
		tableViewer.refresh();
	}

	@Override
	protected void buttonPressed(int buttonId) {
		
		if (buttonId == IDialogConstants.OK_ID){
			formDataAttribute.setId(IDText.getText().trim());
			formDataAttribute.setName(nameText.getText().trim());
			formDataAttribute.setKey(valueKeyText.getText().trim());
			formDataAttribute.setType(typeCombo.getText().trim());
			formDataAttribute.setAccessType(accessTypeCombo.getText().trim());
			formDataAttribute.setRequired(requiredCombo.getText().trim());
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(480,550);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("Edit"));    //$NON-NLS-1$
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}
